package com.example._06servlet; /**
 * @author YAO
 * @create 2021-04-13 22:06
 */

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;

public class ContextServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        1、获取 web.xml 中配置的上下文参数 **context-param
        ServletContext context = getServletConfig().getServletContext();
        String username = context.getInitParameter("username");
        String password = context.getInitParameter("password");
        System.out.println("context参数username的值是：" + username);
        System.out.println("context参数password的值是：" + password);
//        2、获取当前的工程路径，格式: /工程路径
        System.out.println(context.getContextPath());
//        3、获取工程部署后在服务器硬盘上的绝对路径
        // /斜杠被服务器解析地址为http://ip:port/工程名/ 映射到IDEA代码的web目录
        System.out.println("工程部署的路径是:" + context.getRealPath("/"));
        System.out.println("工程css目录的绝对路径是:" + context.getRealPath("/css"));
        System.out.println("工程imgs目录code.bmp的绝对路径是:" + context.getRealPath("/imgs/code.bmp"));
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
